package com.k8.nacosRegistry;

import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.k8.common.event.EventDeployer;
import com.k8.registry.NotifyListener2;
import com.k8.registry.RegistryException;
import com.k8.registry.impl.ListenerRegistry;
import com.k8.rpc.url.ApiSignature;
import com.k8.rpc.url.RegistrySignature;
import com.k8.rpc.url.URL;
import com.k8.rpc.util.RpcUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static com.k8.common.constants.CommonConstants.DATA_ID_MID_FIX;
import static com.k8.common.constants.CommonConstants.K8_GROUP;
import static com.k8.common.constants.CommonKeyConstants.*;

public class TestClass {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        NacosClientProperties prototype = NacosClientProperties.PROTOTYPE;
        NamingService namingService = null;

        Instance instance = new Instance();
        instance.setPort(8080);
        instance.setInstanceId("1.1.1");
        instance.setClusterName("default");
        instance.setIp("127.0.0.1");
        instance.setServiceName("testApp0");
        instance.setHealthy(true);
        instance.setWeight(1);
        prototype.setProperty(SERVER_ADDR, "127.0.0.1:8848");
        Map<String, String> map = new HashMap<>();
        map.put("key1", "name1");
        map.put("key2", "name2");
        map.put("key3", "name3");
        instance.setMetadata(map);
        try {
            namingService = NamingFactory.createNamingService(prototype.asProperties());
            namingService.registerInstance("testApp", K8_GROUP, instance);
        } catch (NacosException e) {
            throw new RegistryException(e.getMessage());
        }
        countDownLatch.await();
    }

    public static void main3(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        int core = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(core, 2 * core, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
        EventDeployer eventDeployer = new EventDeployer(threadPoolExecutor);
        URL url = new URL();
        url.setProtocol("nacos");
        url.setHost("localhost");
        url.setPort(8848);
        url.setPath("com.a.w::");
        url.toString();
        RegistrySignature registrySignature = new RegistrySignature(url);
        String exporterSignature = RpcUtils.getExporterSignature(new ApiSignature(url, ""), registrySignature);
        eventDeployer.addListener(new NotifyListener2(null, url, exporterSignature, null));
        ListenerRegistry.RegistryEventDeployerWrapper registryEventDeployerWrapper = new ListenerRegistry.RegistryEventDeployerWrapper(eventDeployer);
        TestClass testClass = new TestClass();
        testClass.doRegisterMappingAndConfiguration0(url, registryEventDeployerWrapper);
        countDownLatch.await();

    }

    public void doRegisterMappingAndConfiguration0(URL url, ListenerRegistry.RegistryEventDeployerWrapper registryEventDeployerWrapper) {
        String serviceName = "testService";
        String interfaceName = "testInterface";
        String mappingDataId = interfaceName;
        String group = "testGroup";
        String version = "1.0.0";
        String configureDataId = interfaceName + DATA_ID_MID_FIX + group + version;
        NacosClientProperties prototype = NacosClientProperties.PROTOTYPE;
        prototype.setProperty(SERVER_ADDR, url.getAddress().getAddr());
        ConfigService configService = null;
        ApiSignature apiSignature = new ApiSignature(url, "");

    }

    public static void mainInstance(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        URL url = new URL();
        url.setAppName("testService");
        String serviceName = url.getAppName();
        int port = url.getPort(8848);
        String ip = url.getIp();
        String cluster = url.getParameter(CLUSTER_NAME_KEY, "DEFAULT");
        String group = url.getParameter(GROUP_KEY, "DEFAULT_GROUP");
        NacosClientProperties prototype = NacosClientProperties.PROTOTYPE;
        String userName = url.getUserName("nacos");
        String passWord = url.getPassWord("nacos");
        prototype.setProperty(USER_NAME, userName);
        prototype.setProperty(passWord, passWord);
        prototype.setProperty(SERVER_ADDR, ip + ":" + port);
        NamingService namingService = null;
        try {
            namingService = NamingFactory.createNamingService(prototype.asProperties());
            namingService.registerInstance(serviceName, group, ip, port, cluster);
        } catch (NacosException e) {
            throw new RegistryException(e.getMessage());
        }
        countDownLatch.await();
    }
}
